home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / gcc / ixemlsrc.lha / ixemul / ixnet / res_debug.c < prev    next >
C/C++ Source or Header  |  1996-03-13  |  12KB  |  524 lines

  1. /*-
  2.  * Copyright (c) 1985, 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  *
  33.  *    @(#)res_debug.c 5.36 (Berkeley) 3/6/91
  34.  */
  35.  
  36. #if defined(LIBC_SCCS) && !defined(lint)
  37. static char sccsid[] = "@(#)res_debug.c 5.36 (Berkeley) 3/6/91";
  38. #endif /* LIBC_SCCS and not lint */
  39.  
  40. #include <sys/param.h>
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h>
  43. #include <arpa/nameser.h>
  44. #include <resolv.h>
  45. #include <stdio.h>
  46. #include <string.h>
  47.  
  48. void __fp_query();
  49. char *__p_class(), *__p_time(), *__p_type();
  50. static char *p_cdname(), *p_rr();
  51.  
  52. char *_res_opcodes[] = {
  53.     "QUERY",
  54.     "IQUERY",
  55.     "CQUERYM",
  56.     "CQUERYU",
  57.     "4",
  58.     "5",
  59.     "6",
  60.     "7",
  61.     "8",
  62.     "UPDATEA",
  63.     "UPDATED",
  64.     "UPDATEDA",
  65.     "UPDATEM",
  66.     "UPDATEMA",
  67.     "ZONEINIT",
  68.     "ZONEREF",
  69. };
  70.  
  71. char *_res_resultcodes[] = {
  72.     "NOERROR",
  73.     "FORMERR",
  74.     "SERVFAIL",
  75.     "NXDOMAIN",
  76.     "NOTIMP",
  77.     "REFUSED",
  78.     "6",
  79.     "7",
  80.     "8",
  81.     "9",
  82.     "10",
  83.     "11",
  84.     "12",
  85.     "13",
  86.     "14",
  87.     "NOCHANGE",
  88. };
  89.  
  90. void
  91. __p_query(msg)
  92.     char *msg;
  93. {
  94.     __fp_query(msg,stdout);
  95. }
  96.  
  97. /*
  98.  * Print the contents of a query.
  99.  * This is intended to be primarily a debugging routine.
  100.  */
  101. void
  102. __fp_query(msg,file)
  103.     char *msg;
  104.     FILE *file;
  105. {
  106.     register char *cp;
  107.     register HEADER *hp;
  108.     register int n;
  109.  
  110.     /*
  111.      * Print header fields.
  112.      */
  113.     hp = (HEADER *)msg;
  114.     cp = msg + sizeof(HEADER);
  115.     fprintf(file,"HEADER:\n");
  116.     fprintf(file,"\topcode = %s", _res_opcodes[hp->opcode]);
  117.     fprintf(file,", id = %d", ntohs(hp->id));
  118.     fprintf(file,", rcode = %s\n", _res_resultcodes[hp->rcode]);
  119.     fprintf(file,"\theader flags: ");
  120.     if (hp->qr)
  121.         fprintf(file," qr");
  122.     if (hp->aa)
  123.         fprintf(file," aa");
  124.     if (hp->tc)
  125.         fprintf(file," tc");
  126.     if (hp->rd)
  127.         fprintf(file," rd");
  128.     if (hp->ra)
  129.         fprintf(file," ra");
  130.     if (hp->pr)
  131.         fprintf(file," pr");
  132.     fprintf(file,"\n\tqdcount = %d", ntohs(hp->qdcount));
  133.     fprintf(file,", ancount = %d", ntohs(hp->ancount));
  134.     fprintf(file,", nscount = %d", ntohs(hp->nscount));
  135.     fprintf(file,", arcount = %d\n\n", ntohs(hp->arcount));
  136.     /*
  137.      * Print question records.
  138.      */
  139.         n = ntohs(hp->qdcount);
  140.         if (n) {
  141.         fprintf(file,"QUESTIONS:\n");
  142.         while (--n >= 0) {
  143.             fprintf(file,"\t");
  144.             cp = p_cdname(cp, msg, file);
  145.             if (cp == NULL)
  146.                 return;
  147.             fprintf(file,", type = %s", __p_type(_getshort(cp)));
  148.             cp += sizeof(u_short);
  149.             fprintf(file,
  150.                 ", class = %s\n\n", __p_class(_getshort(cp)));
  151.             cp += sizeof(u_short);
  152.         }
  153.     }
  154.     /*
  155.      * Print authoritative answer records
  156.      */
  157.         n = ntohs(hp->ancount);
  158.         if (n) {
  159.         fprintf(file,"ANSWERS:\n");
  160.         while (--n >= 0) {
  161.             fprintf(file,"\t");
  162.             cp = p_rr(cp, msg, file);
  163.             if (cp == NULL)
  164.                 return;
  165.         }
  166.     }
  167.     /*
  168.      * print name server records
  169.      */
  170.         n = ntohs(hp->nscount);
  171.         if (n) {
  172.         fprintf(file,"NAME SERVERS:\n");
  173.         while (--n >= 0) {
  174.             fprintf(file,"\t");
  175.             cp = p_rr(cp, msg, file);
  176.             if (cp == NULL)
  177.                 return;
  178.         }
  179.     }
  180.     /*
  181.      * print additional records
  182.      */
  183.         n = ntohs(hp->arcount);
  184.         if (n) {
  185.         fprintf(file,"ADDITIONAL RECORDS:\n");
  186.         while (--n >= 0) {
  187.             fprintf(file,"\t");
  188.             cp = p_rr(cp, msg, file);
  189.             if (cp == NULL)
  190.                 return;
  191.         }
  192.     }
  193. }
  194.  
  195. static char *
  196. p_cdname(cp, msg, file)
  197.     char *cp, *msg;
  198.     FILE *file;
  199. {
  200.     char name[MAXDNAME];
  201.     int n;
  202.  
  203.     if ((n = dn_expand((u_char *)msg, (u_char *)msg + 512, (u_char *)cp,
  204.         (u_char *)name, sizeof(name))) < 0)
  205.         return (NULL);
  206.     if (name[0] == '\0') {
  207.         name[0] = '.';
  208.         name[1] = '\0';
  209.     }
  210.     fputs(name, file);
  211.     return (cp + n);
  212. }
  213.  
  214. /*
  215.  * Print resource record fields in human readable form.
  216.  */
  217. static char *
  218. p_rr(char *cp, char *msg, FILE *file) {
  219.     int type, class, dlen, n, c;
  220.     struct in_addr inaddr;
  221.     char *cp1, *cp2;
  222.  
  223.     if ((cp = p_cdname(cp, msg, file)) == NULL)
  224.         return (NULL);                  /* compression error */
  225.     fprintf(file,"\n\ttype = %s", __p_type(type = _getshort(cp)));
  226.     cp += sizeof(u_short);
  227.     fprintf(file,", class = %s", __p_class(class = _getshort(cp)));
  228.     cp += sizeof(u_short);
  229.     fprintf(file,", ttl = %s", __p_time(_getlong(cp)));
  230.     cp += sizeof(u_long);
  231.     fprintf(file,", dlen = %d\n", dlen = _getshort(cp));
  232.     cp += sizeof(u_short);
  233.     cp1 = cp;
  234.     /*
  235.     * Print type specific data, if appropriate
  236.     */
  237.     switch (type) {
  238.     case T_A:
  239.         switch (class) {
  240.         case C_IN:
  241.         case C_HS:
  242.             bcopy(cp, (char *)&inaddr, sizeof(inaddr));
  243.             if (dlen == 4) {
  244.                 fprintf(file,"\tinternet address = %s\n",
  245.                     inet_ntoa(inaddr));
  246.                 cp += dlen;
  247.             } else if (dlen == 7) {
  248.                 fprintf(file,"\tinternet address = %s",
  249.                     inet_ntoa(inaddr));
  250.                 fprintf(file,", protocol = %d", cp[4]);
  251.                 fprintf(file,", port = %d\n",
  252.                     (cp[5] << 8) + cp[6]);
  253.                 cp += dlen;
  254.             }
  255.             break;
  256.         default:
  257.             cp += dlen;
  258.         }
  259.         break;
  260.     case T_CNAME:
  261.     case T_MB:
  262.     case T_MG:
  263.     case T_MR:
  264.     case T_NS:
  265.     case T_PTR:
  266.         fprintf(file,"\tdomain name = ");
  267.         cp = p_cdname(cp, msg, file);
  268.         fprintf(file,"\n");
  269.         break;
  270.  
  271.     case T_HINFO:
  272.             n = *cp++;
  273.             if (n) {
  274.             fprintf(file,"\tCPU=%.*s\n", n, cp);
  275.             cp += n;
  276.         }
  277.             n = *cp++;
  278.             if (n) {
  279.             fprintf(file,"\tOS=%.*s\n", n, cp);
  280.             cp += n;
  281.         }
  282.         break;
  283.  
  284.     case T_SOA:
  285.         fprintf(file,"\torigin = ");
  286.         cp = p_cdname(cp, msg, file);
  287.         fprintf(file,"\n\tmail addr = ");
  288.         cp = p_cdname(cp, msg, file);
  289.             fprintf(file,"\n\tserial = %ld", (long)_getlong(cp));
  290.         cp += sizeof(u_long);
  291.         fprintf(file,"\n\trefresh = %s", __p_time(_getlong(cp)));
  292.         cp += sizeof(u_long);
  293.         fprintf(file,"\n\tretry = %s", __p_time(_getlong(cp)));
  294.         cp += sizeof(u_long);
  295.         fprintf(file,"\n\texpire = %s", __p_time(_getlong(cp)));
  296.         cp += sizeof(u_long);
  297.         fprintf(file,"\n\tmin = %s\n", __p_time(_getlong(cp)));
  298.         cp += sizeof(u_long);
  299.         break;
  300.  
  301.     case T_MX:
  302.             fprintf(file,"\tpreference = %d,",_getshort(cp));
  303.         cp += sizeof(u_short);
  304.         fprintf(file," name = ");
  305.         cp = p_cdname(cp, msg, file);
  306.         break;
  307.  
  308.     case T_TXT:
  309.         (void) fputs("\t\"", file);
  310.         cp2 = cp1 + dlen;
  311.         while (cp < cp2) {
  312.                     n = (unsigned char) *cp++;
  313.                     if (n) {
  314.                 for (c = n; c > 0 && cp < cp2; c--)
  315.                     if (*cp == '\n') {
  316.                     (void) putc('\\', file);
  317.                     (void) putc(*cp++, file);
  318.                     } else
  319.                     (void) putc(*cp++, file);
  320.             }
  321.         }
  322.         (void) fputs("\"\n", file);
  323.         break;
  324.  
  325.     case T_MINFO:
  326.         fprintf(file,"\trequests = ");
  327.         cp = p_cdname(cp, msg, file);
  328.         fprintf(file,"\n\terrors = ");
  329.         cp = p_cdname(cp, msg, file);
  330.         break;
  331.  
  332.     case T_UINFO:
  333.         fprintf(file,"\t%s\n", cp);
  334.         cp += dlen;
  335.         break;
  336.  
  337.     case T_UID:
  338.     case T_GID:
  339.         if (dlen == 4) {
  340.             fprintf(file,"\t%ld\n", _getlong(cp));
  341.             cp += sizeof(int);
  342.         }
  343.         break;
  344.  
  345.     case T_WKS:
  346.         if (dlen < sizeof(u_long) + 1)
  347.             break;
  348.         bcopy(cp, (char *)&inaddr, sizeof(inaddr));
  349.         cp += sizeof(u_long);
  350.         fprintf(file,"\tinternet address = %s, protocol = %d\n\t",
  351.             inet_ntoa(inaddr), *cp++);
  352.         n = 0;
  353.         while (cp < cp1 + dlen) {
  354.             c = *cp++;
  355.             do {
  356.                 if (c & 0200)
  357.                     fprintf(file," %d", n);
  358.                 c <<= 1;
  359.             } while (++n & 07);
  360.         }
  361.         putc('\n',file);
  362.         break;
  363.  
  364. #ifdef ALLOW_T_UNSPEC
  365.     case T_UNSPEC:
  366.         {
  367.             int NumBytes = 8;
  368.             char *DataPtr;
  369.             int i;
  370.  
  371.             if (dlen < NumBytes) NumBytes = dlen;
  372.             fprintf(file, "\tFirst %d bytes of hex data:",
  373.                 NumBytes);
  374.             for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++)
  375.                             fprintf(file, " %x", (unsigned)*DataPtr);
  376.             fputs("\n", file);
  377.             cp += dlen;
  378.         }
  379.         break;
  380. #endif /* ALLOW_T_UNSPEC */
  381.  
  382.     default:
  383.         fprintf(file,"\t???\n");
  384.         cp += dlen;
  385.     }
  386.     if (cp != cp1 + dlen) {
  387.             fprintf(file,"packet size error (%#x != %#x)\n", (unsigned)cp, (unsigned)cp1+dlen);
  388.         cp = NULL;
  389.     }
  390.     fprintf(file,"\n");
  391.     return (cp);
  392. }
  393.  
  394. static    char nbuf[40];
  395.  
  396. /*
  397.  * Return a string for the type
  398.  */
  399. char *
  400. __p_type(type)
  401.     int type;
  402. {
  403.     switch (type) {
  404.     case T_A:
  405.         return("A");
  406.     case T_NS:        /* authoritative server */
  407.         return("NS");
  408.     case T_CNAME:        /* canonical name */
  409.         return("CNAME");
  410.     case T_SOA:        /* start of authority zone */
  411.         return("SOA");
  412.     case T_MB:        /* mailbox domain name */
  413.         return("MB");
  414.     case T_MG:        /* mail group member */
  415.         return("MG");
  416.     case T_MR:        /* mail rename name */
  417.         return("MR");
  418.     case T_NULL:        /* null resource record */
  419.         return("NULL");
  420.     case T_WKS:        /* well known service */
  421.         return("WKS");
  422.     case T_PTR:        /* domain name pointer */
  423.         return("PTR");
  424.     case T_HINFO:        /* host information */
  425.         return("HINFO");
  426.     case T_MINFO:        /* mailbox information */
  427.         return("MINFO");
  428.     case T_MX:        /* mail routing info */
  429.         return("MX");
  430.     case T_TXT:        /* text */
  431.         return("TXT");
  432.     case T_AXFR:        /* zone transfer */
  433.         return("AXFR");
  434.     case T_MAILB:        /* mail box */
  435.         return("MAILB");
  436.     case T_MAILA:        /* mail address */
  437.         return("MAILA");
  438.     case T_ANY:        /* matches any type */
  439.         return("ANY");
  440.     case T_UINFO:
  441.         return("UINFO");
  442.     case T_UID:
  443.         return("UID");
  444.     case T_GID:
  445.         return("GID");
  446. #ifdef ALLOW_T_UNSPEC
  447.     case T_UNSPEC:
  448.         return("UNSPEC");
  449. #endif /* ALLOW_T_UNSPEC */
  450.     default:
  451.         (void)sprintf(nbuf, "%d", type);
  452.         return(nbuf);
  453.     }
  454. }
  455.  
  456. /*
  457.  * Return a mnemonic for class
  458.  */
  459. char *
  460. __p_class(class)
  461.     int class;
  462. {
  463.  
  464.     switch (class) {
  465.     case C_IN:        /* internet class */
  466.         return("IN");
  467.     case C_HS:        /* hesiod class */
  468.         return("HS");
  469.     case C_ANY:        /* matches any class */
  470.         return("ANY");
  471.     default:
  472.         (void)sprintf(nbuf, "%d", class);
  473.         return(nbuf);
  474.     }
  475. }
  476.  
  477. /*
  478.  * Return a mnemonic for a time to live
  479.  */
  480. char *
  481. __p_time(value)
  482.     u_long value;
  483. {
  484.     int secs, mins, hours;
  485.     register char *p;
  486.  
  487.     if (value == 0) {
  488.         strcpy(nbuf, "0 secs");
  489.         return(nbuf);
  490.     }
  491.  
  492.     secs = value % 60;
  493.     value /= 60;
  494.     mins = value % 60;
  495.     value /= 60;
  496.     hours = value % 24;
  497.     value /= 24;
  498.  
  499. #define PLURALIZE(x)    x, (x == 1) ? "" : "s"
  500.     p = nbuf;
  501.     if (value) {
  502.                 (void)sprintf(p, "%ld day%s", PLURALIZE(value));
  503.         while (*++p);
  504.     }
  505.     if (hours) {
  506.         if (value)
  507.             *p++ = ' ';
  508.         (void)sprintf(p, "%d hour%s", PLURALIZE(hours));
  509.         while (*++p);
  510.     }
  511.     if (mins) {
  512.         if (value || hours)
  513.             *p++ = ' ';
  514.         (void)sprintf(p, "%d min%s", PLURALIZE(mins));
  515.         while (*++p);
  516.     }
  517.     if (secs || ! (value || hours || mins)) {
  518.         if (value || hours || mins)
  519.             *p++ = ' ';
  520.         (void)sprintf(p, "%d sec%s", PLURALIZE(secs));
  521.     }
  522.     return(nbuf);
  523. }
  524.